<!doctype html>
<html lang="zh-Hans">
	<head>
		<title>电阻色环计算器 - 嵌入式工具集</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<!-- 以下为浏览器中的配置，正式 编译之后请注释掉这段-->
		<link rel="stylesheet" href="../styles/styles.css" />

		<!-- 以下为LCEDA中的配置，正式 编译之后请勿注释这段-->
		<link rel="stylesheet" href="/iframe/styles/styles.css" />

		<style>
			/* 电阻色环计算器专用样式 */
			.resistor-container {
				position: relative;
				height: 120px;
				display: flex;
				align-items: center;
				justify-content: center;
				margin: 2rem 0;
			}

			.resistor-body {
				position: relative;
				width: 240px;
				height: 60px;
				background: #d4d4d4;
				border: 1px solid #b0b0b0;
				border-radius: 4px;
				display: flex;
				align-items: center;
				justify-content: space-around;
			}

			.resistor-body::before,
			.resistor-body::after {
				content: '';
				position: absolute;
				top: 50%;
				transform: translateY(-50%);
				width: 20px;
				height: 80px;
				background: #a0a0a0;
			}

			.resistor-body::before {
				left: -15px;
				border-radius: 8px 0 0 8px;
			}

			.resistor-body::after {
				right: -15px;
				border-radius: 0 8px 8px 0;
			}

			.color-band {
				width: 16px;
				height: 60px;
				border-radius: 2px;
				cursor: pointer;
				transition: all 0.2s ease;
				position: relative;
				z-index: 1;
			}

			.color-band:hover {
				transform: scale(1.1);
				box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
			}

			.color-band.active {
				border: 2px solid #4a90e2;
			}

			.color-palette {
				max-width: 100%;
				display: flex;
				flex-wrap: wrap;
				gap: 10px;
				margin: 1rem 0;
				justify-content: center;
			}

			.color-option {
				flex: 0 0 calc(16.666% - 10px);
				max-width: calc(16.666% - 10px);
				text-align: center;
				cursor: pointer;
				transition: transform 0.2s ease;
				padding: 8px 4px;
				border-radius: 6px;
			}

			.color-option:hover {
				transform: translateY(-3px);
				background-color: rgba(240, 240, 240, 0.5);
			}

			.color-option .color-swatch {
				height: 30px;
				border-radius: 4px;
				margin-bottom: 6px;
				border: 1px solid #e0e0e0;
				transition: box-shadow 0.2s ease;
			}

			.color-option:hover .color-swatch {
				box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
			}

			.color-option .color-black {
				font-size: 12px;
				color: black;
			}
			.color-option .color-brown {
				font-size: 12px;
				color: brown;
			}
			.color-option .color-red {
				font-size: 12px;
				color: red;
			}
			.color-option .color-orange {
				font-size: 12px;
				color: orange;
			}
			.color-option .color-yellow {
				font-size: 12px;
				color: yellow;
			}
			.color-option .color-green {
				font-size: 12px;
				color: green;
			}
			.color-option .color-blue {
				font-size: 12px;
				color: blue;
			}
			.color-option .color-violet {
				font-size: 12px;
				color: violet;
			}
			.color-option .color-gray {
				font-size: 12px;
				color: gray;
			}
			.color-option .color-white {
				font-size: 12px;
				color: white;
			}
			.color-option .color-gold {
				font-size: 12px;
				color: gold;
			}
			.color-option .color-silver {
				font-size: 12px;
				color: silver;
			}
			.color-option .color-name {
				font-size: 12px;
				color: #333;
			}

			.result-card {
				background: white;
				border-radius: 12px;
				padding: 1.5rem;
				box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
				margin: 1.5rem 0;
				transition: transform 0.2s ease;
			}

			.result-card:hover {
				transform: translateY(-2px);
				box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
			}

			.resistance-value {
				font-size: 2rem;
				font-weight: 700;
				text-align: center;
				color: #2c3e50;
				margin: 0.5rem 0;
			}

			.radio-group {
				display: flex;
				justify-content: center;
				gap: 2rem;
				margin: 1rem 0;
			}

			.radio-label {
				display: flex;
				align-items: center;
				cursor: pointer;
				transition: color 0.2s ease;
			}

			.radio-label:hover {
				color: #4a90e2;
			}

			.btn-reset {
				display: block;
				width: 100%;
				padding: 0.75rem;
				background-color: #f5f5f5;
				color: #555;
				border: none;
				border-radius: 8px;
				font-size: 1rem;
				font-weight: 500;
				cursor: pointer;
				transition: all 0.2s ease;
				text-align: center;
			}

			.btn-reset:hover {
				background-color: #e0e0e0;
				transform: translateY(-1px);
			}

			.band-info {
				text-align: center;
				margin-top: 0.5rem;
				font-size: 0.875rem;
				color: #666;
			}

			/* 响应式设计 */
			@media (max-width: 640px) {
				.resistor-container {
					height: 100px;
				}

				.resistor-body {
					width: 200px;
					height: 50px;
				}

				.color-band {
					width: 14px;
					height: 50px;
				}

				.color-option {
					flex: 0 0 calc(25% - 10px);
					max-width: calc(25% - 10px);
				}

				.resistance-value {
					font-size: 1.5rem;
				}

				.radio-group {
					flex-direction: column;
					align-items: center;
					gap: 1rem;
				}
			}
		</style>
	</head>
	<body class="bg-gradient-to-br from-light to-blue-50 min-h-screen p-4 md:p-6 font-sans">
		<div class="max-w-md mx-auto bg-white rounded-2xl shadow-lg p-6 md:p-8">
			<h2 class="text-2xl md:text-3xl font-bold text-primary mb-6 text-center">电阻色环计算器</h2>

			<div class="mb-6">
				<label class="block text-sm font-medium text-gray-700 mb-3 text-center">选择电阻类型</label>
				<div class="radio-group">
					<label class="radio-label">
						<input type="radio" name="resistor-type" value="4-band" class="form-radio h-5 w-5 text-accent" checked />
						<span class="ml-2">4色环</span>
					</label>
					<label class="radio-label">
						<input type="radio" name="resistor-type" value="5-band" class="form-radio h-5 w-5 text-accent" />
						<span class="ml-2">5色环</span>
					</label>
				</div>
			</div>

			<div class="mb-6">
				<div class="resistor-container">
					<div class="resistor-body">
						<div
							id="band-1"
							class="color-band"
							style="background-color: #000000; background: #000000"
							data-color="black"
							title="黑色: 0"
						></div>
						<div
							id="band-2"
							class="color-band"
							style="background-color: #8b4513; background: #8b4513"
							data-color="brown"
							title="棕色: 1"
						></div>
						<div
							id="band-3"
							class="color-band"
							style="background-color: #ff0000; background: #ff0000"
							data-color="red"
							title="红色: 2"
						></div>
						<div
							id="band-4"
							class="color-band"
							style="background-color: #ffd700; background: #ffd700"
							data-color="gold"
							title="金色: ±5%"
						></div>
						<div
							id="band-5"
							class="color-band hidden"
							style="background-color: #a9a9a9; background: #a9a9a9"
							data-color="none"
							title="无: ±20%"
						></div>
					</div>
				</div>
				<p class="band-info">点击色环选择颜色，或直接点击下方颜色选项</p>

				<div class="color-palette">
					<div class="color-option" data-color="black">
						<div class="color-swatch" style="background-color: #000000"></div>
						<span class="color-name">黑色</span>
					</div>
					<div class="color-option" data-color="brown">
						<div class="color-swatch" style="background-color: #8b4513"></div>
						<span class="color-name">棕色</span>
					</div>
					<div class="color-option" data-color="red">
						<div class="color-swatch" style="background-color: #ff0000"></div>
						<span class="color-name">红色</span>
					</div>
					<div class="color-option" data-color="orange">
						<div class="color-swatch" style="background-color: #ffa500"></div>
						<span class="color-name">橙色</span>
					</div>
					<div class="color-option" data-color="yellow">
						<div class="color-swatch" style="background-color: #ffff00"></div>
						<span class="color-name">黄色</span>
					</div>
					<div class="color-option" data-color="green">
						<div class="color-swatch" style="background-color: #008000"></div>
						<span class="color-name">绿色</span>
					</div>
					<div class="color-option" data-color="blue">
						<div class="color-swatch" style="background-color: #0000ff"></div>
						<span class="color-name">蓝色</span>
					</div>
					<div class="color-option" data-color="violet">
						<div class="color-swatch" style="background-color: #800080"></div>
						<span class="color-name">紫色</span>
					</div>
					<div class="color-option" data-color="gray">
						<div class="color-swatch" style="background-color: #808080"></div>
						<span class="color-name">灰色</span>
					</div>
					<div class="color-option" data-color="white">
						<div class="color-swatch" style="background-color: #ffffff; border: 1px solid #cccccc"></div>
						<span class="color-name">白色</span>
					</div>
					<div class="color-option" data-color="gold">
						<div class="color-swatch" style="background-color: #ffd700"></div>
						<span class="color-name">金色</span>
					</div>
					<div class="color-option" data-color="silver">
						<div class="color-swatch" style="background-color: #c0c0c0"></div>
						<span class="color-name">银色</span>
					</div>
				</div>
			</div>

			<div class="result-card">
				<h3 class="font-bold text-primary mb-2 text-center">计算结果</h3>
				<p class="resistance-value" id="resistance-value">0 Ω ±5%</p>
			</div>

			<button onclick="resistanceClear()" class="btn-reset">🔄 重置</button>
		</div>

		<script>
			// 电阻色环计算
			function initResistorColorCode() {
				// 颜色代码映射
				const colorCodes = {
					'black': 0,
					'brown': 1,
					'red': 2,
					'orange': 3,
					'yellow': 4,
					'green': 5,
					'blue': 6,
					'violet': 7,
					'gray': 8,
					'white': 9,
				};

				// 乘数映射
				const multiplierCodes = {
					'black': 1,
					'brown': 10,
					'red': 100,
					'orange': 1000,
					'yellow': 10000,
					'green': 100000,
					'blue': 1000000,
					'violet': 10000000,
					'gray': 100000000,
					'white': 1000000000,
					'gold': 0.1,
					'silver': 0.01,
				};

				// 误差映射
				const toleranceCodes = {
					'brown': '±1%',
					'red': '±2%',
					'green': '±0.5%',
					'blue': '±0.25%',
					'violet': '±0.1%',
					'gray': '±0.05%',
					'gold': '±5%',
					'silver': '±10%',
					'none': '±20%',
				};

				// 颜色显示映射
				const colorDisplay = {
					'black': 'bg-black',
					'brown': 'bg-[#8B4513]',
					'red': 'bg-red-600',
					'orange': 'bg-orange-500',
					'yellow': 'bg-yellow-400',
					'green': 'bg-green-600',
					'blue': 'bg-blue-600',
					'violet': 'bg-purple-600',
					'gray': 'bg-gray-500',
					'white': 'bg-white border border-gray-300',
					'gold': 'bg-yellow-500',
					'silver': 'bg-gray-300',
					'none': 'bg-gray-400',
				};

				// 当前活动的色环
				let activeBand = null;

				// 为色环添加点击事件，使其可以直接选择
				document.querySelectorAll('.color-band').forEach((band) => {
					band.addEventListener('click', () => {
						// 移除所有色环的活动状态
						document.querySelectorAll('.color-band').forEach((b) => {
							b.classList.remove('active');
						});

						// 设置当前色环为活动状态
						band.classList.add('active');
						activeBand = band;
					});
				});

				// 颜色选项点击事件
				document.querySelectorAll('.color-option').forEach((option) => {
					option.addEventListener('click', () => {
						const selectedColor = option.getAttribute('data-color');

						// 如果有活动的色环，或者点击颜色选项时将颜色应用到第一个可点击的色环
						if (activeBand) {
							updateBandColor(activeBand, selectedColor);
						} else {
							// 默认选择第一个可见的色环
							const visibleBands = document.querySelectorAll('.color-band:not(.hidden)');
							if (visibleBands.length > 0) {
								visibleBands[0].classList.add('active');
								activeBand = visibleBands[0];
								updateBandColor(activeBand, selectedColor);
							}
						}
					});
				});

				// 更新色环颜色的辅助函数
				function updateBandColor(band, color) {
					// 颜色到十六进制值的映射（内部定义以确保作用域正确）
					const colorHexValues = {
						'black': '#000000',
						'brown': '#8B4513',
						'red': '#FF0000',
						'orange': '#FFA500',
						'yellow': '#FFFF00',
						'green': '#008000',
						'blue': '#0000FF',
						'violet': '#800080',
						'gray': '#808080',
						'white': '#FFFFFF',
						'gold': '#FFD700',
						'silver': '#C0C0C0',
						'none': '#808080',
					};

					// 移除之前的颜色类
					let classes = band.className.split(' ').filter((cls) => !cls.startsWith('bg-'));
					band.className = classes.join(' ');

					// 直接设置背景颜色，确保不为空
					const bgColor = colorHexValues[color] || '#8B4513'; // 默认棕色，确保不会透明
					band.style.backgroundColor = bgColor;
					band.style.background = bgColor; // 同时设置background属性以确保兼容性

					// 为白色添加边框
					if (color === 'white') {
						band.style.border = '1px solid #CCCCCC';
					} else {
						band.style.border = 'none';
					}

					band.setAttribute('data-color', color);
					band.setAttribute('title', getColorTitle(color));

					// 添加轻微的视觉反馈
					band.style.transform = 'scale(1.2)';
					setTimeout(() => {
						band.style.transform = '';
					}, 200);

					// 计算电阻值
					calculateResistance();
				}

				// 初始化颜色选择器的颜色显示
				function initColorSwatches() {
					document.querySelectorAll('.color-option').forEach((option) => {
						const color = option.getAttribute('data-color');
						const swatch = option.querySelector('.color-swatch');
						// 移除所有背景类
						let classes = swatch.className
							.split(' ')
							.filter((cls) => !cls.startsWith('bg-') && cls !== 'border' && !cls.includes('border-'));
						swatch.className = classes.join(' ');
						// 添加正确的背景色
						swatch.className += ' ' + colorDisplay[color];
						// 为白色添加边框以确保可见性
						if (color === 'white') {
							swatch.className += ' border border-gray-300';
						}
					});
				}

				// 电阻类型切换
				document.querySelectorAll('input[name="resistor-type"]').forEach((radio) => {
					radio.addEventListener('change', () => {
						const band5 = document.getElementById('band-5');
						if (radio.value === '5-band') {
							band5.classList.remove('hidden');
						} else {
							band5.classList.add('hidden');
						}

						// 重置活动色环
						if (activeBand && activeBand.id === 'band-5' && radio.value !== '5-band') {
							activeBand.classList.remove('active');
							activeBand = null;
						}

						calculateResistance();
					});
				});

				// 计算电阻值
				function calculateResistance() {
					const is5Band = document.querySelector('input[name="resistor-type"][value="5-band"]').checked;
					const band1Color = document.getElementById('band-1').getAttribute('data-color');
					const band2Color = document.getElementById('band-2').getAttribute('data-color');
					const band3Color = document.getElementById('band-3').getAttribute('data-color');
					const band4Color = document.getElementById('band-4').getAttribute('data-color');
					const band5Color = document.getElementById('band-5').getAttribute('data-color');

					let resistance, tolerance;

					if (is5Band) {
						// 5色环计算
						const significant1 = colorCodes[band1Color];
						const significant2 = colorCodes[band2Color];
						const significant3 = colorCodes[band3Color];
						const multiplier = multiplierCodes[band4Color];
						tolerance = toleranceCodes[band5Color];

						resistance = (significant1 * 100 + significant2 * 10 + significant3) * multiplier;
					} else {
						// 4色环计算
						const significant1 = colorCodes[band1Color];
						const significant2 = colorCodes[band2Color];
						const multiplier = multiplierCodes[band3Color];
						tolerance = toleranceCodes[band4Color];

						resistance = (significant1 * 10 + significant2) * multiplier;
					}

					// 格式化显示
					let displayValue;
					if (resistance >= 1000000) {
						displayValue = (resistance / 1000000).toFixed(2) + ' MΩ';
					} else if (resistance >= 1000) {
						displayValue = (resistance / 1000).toFixed(2) + ' kΩ';
					} else {
						displayValue = resistance.toFixed(2) + ' Ω';
					}

					// 添加结果更新的动画效果
					const resultElement = document.getElementById('resistance-value');
					resultElement.style.opacity = '0.5';
					setTimeout(() => {
						resultElement.textContent = displayValue + ' ' + tolerance;
						resultElement.style.opacity = '1';
					}, 150);
				}

				function getColorTitle(color) {
					const titles = {
						'black': '黑色: 0',
						'brown': '棕色: 1',
						'red': '红色: 2',
						'orange': '橙色: 3',
						'yellow': '黄色: 4',
						'green': '绿色: 5',
						'blue': '蓝色: 6',
						'violet': '紫色: 7',
						'gray': '灰色: 8',
						'white': '白色: 9',
						'gold': '金色: ±5%',
						'silver': '银色: ±10%',
						'none': '无: ±20%',
					};
					return titles[color] || color;
				}

				// 初始化颜色选择器
				initColorSwatches();
				// 初始化计算
				calculateResistance();

				// 返回公开方法
				return {
					calculateResistance: calculateResistance,
				};
			}

			function resistanceClear() {
				// 重置为默认状态
				document.getElementById('band-1').setAttribute('data-color', 'black');
				document.getElementById('band-2').setAttribute('data-color', 'brown');
				document.getElementById('band-3').setAttribute('data-color', 'red');
				document.getElementById('band-4').setAttribute('data-color', 'gold');
				document.getElementById('band-5').setAttribute('data-color', 'none');

				// 重置颜色显示和活动状态
				document.getElementById('band-1').className = 'color-band bg-black';
				document.getElementById('band-2').className = 'color-band bg-[#8B4513]';
				document.getElementById('band-3').className = 'color-band bg-red-600';
				document.getElementById('band-4').className = 'color-band bg-yellow-500';
				document.getElementById('band-5').className = 'color-band hidden bg-gray-400';

				// 重置选择器
				document.querySelector('input[name="resistor-type"][value="4-band"]').checked = true;

				// 重新初始化并计算
				const resistorCalculator = initResistorColorCode();
				resistorCalculator.calculateResistance();
			}

			// 页面加载时初始化
			window.onload = function () {
				initResistorColorCode();
			};

			// 暴露给父页面的接口
			window.resistanceClear = resistanceClear;
		</script>
	</body>
</html>
